import Vuex from 'vuex';
import Vue from 'vue';
import _ from 'lodash';
import api from './api/index';
Vue.use(Vuex);



let store = new Vuex.Store({
    state: {
        menus: []
    },
    mutations: {
        activeMenu(state, currentRoute) {
            let path = currentRoute.path;
            state.menus = _.map(state.menus, item => {
                item.active = false;
                if (item.url === path)
                    item.active = true;
                if (item.hasChildren)
                    item.children = _.map(item.children, subitem => {
                        if (subitem.url === path)
                            subitem.active = true;
                        return subitem;
                    });
                return item;
            });
        },
        collapse(state, title) {
            state.menus = _.map(state.menus, item => {
                if (item.title === title && item.hasChildren)
                    item.open = !item.open;
                return item;
            });
        },
        getAll(state,res){
            state.menus=res.data;
        }
    },
    actions:{
        activeMenu({commit},currentRoute){
            commit('activeMenu',currentRoute);
        },
        getAll({commit}){
            api.getAll().then(res=>{
                commit('getAll',res);
            });
        }
    }
});
store.dispatch('getAll');
export default store;